package com.example.tliaswebmanagement.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class JwtUtils {
    private static final Key signKey = Keys.secretKeyFor(SignatureAlgorithm.HS256);
    private static Long expire = 43200000L;

    //生成jwt令牌
    public static String generateJwtToken(Map<String,Object> claims){
        String jwt= Jwts.builder()
                .addClaims(claims)
                .signWith(signKey)
                .setExpiration(new Date(System.currentTimeMillis()+expire))
                .compact();
        return jwt;
    }
    //解析jwt令牌
    public static Map<String,Object> parseJwtToken(String jwt){
        Map<String,Object> claims= Jwts.parser()
                .setSigningKey(signKey)
                .build()
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }
}
